package com.product.magina.dingtalk.controller;

import com.alibaba.fastjson.JSONObject;
import com.magina.common.model.DTJsSignatureModel;
import com.magina.common.response.Response;
import com.product.magina.dingtalk.service.DingTalkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
    @RequestMapping(value = "dingtalk")
@Slf4j
public class DingTalkController {

    @Autowired
    private DingTalkService dingTalkService;

    @RequestMapping(value = "/checksignature")
    public Response checkSignature(HttpServletRequest request, HttpServletResponse response) throws Exception {
        log.info("=============================进入checkSignature==================================");
//        String code = request.getParameter("code");
//        if(!StringUtils.isEmpty(code)){
//            //开启免登录
//            log.info("开始免登录获取用户信息!");
//            JSONObject userInfo = dingTalkService.getDingTalkUserInfo(code);
//            return Response.succeed(userInfo);
//        }
        log.info("开始签名校验");
        //返回拼接的 jsticket,signature,nonceStr,corpId,timeStamp,agentId 的json
        DTJsSignatureModel json =  dingTalkService.getConfig(request);
        return Response.succeed(json);
    }

    @RequestMapping(value = "/getDTUserInfo")
    public Response getDTUserInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        log.info("=============================getDTUserInfo==================================");
        String code = request.getParameter("code");
        if(!StringUtils.isEmpty(code)){
            //开启免登录
            log.info("开始免登录获取用户信息!");
            JSONObject userInfo = dingTalkService.getDingTalkUserInfo(code);
            return Response.succeed(userInfo);
        }
        return Response.fail("code为空!");
    }


}
